<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//理财平台表
class Plat_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->plat = 'plat';
        $this->plat_info = 'plat_info';
        $this->plat_interface = 'plat_interface';
        $this->plat_rule = 'plat_rule';
        $this->plat_log = 'plat_log';
        $this->users = 'users';
        $this->plat_cooperation = 'cooperation';
        $this->plat_baseinfo = 'plat_baseinfo';
    }
    
    function get($where){
        $this->db->select($this->plat.'.*');
        $this->db->from($this->plat);
        $this->db->where($where);
        $query = $this->db->get();
        return $query->row_array(); 
    }
    
    function get_info($where){
        $this->db->select($this->plat_info.'.*');
        $this->db->from($this->plat_info);
        $this->db->where($where);
        $query = $this->db->get();
        return $query->row_array(); 
    }
    
    function getPlatAll($where,$page,$limit){
        $this->db->select($this->plat.'.*');
        $this->db->from($this->plat);
        $this->db->where($where);
        $this->db->order_by("plat.sort", "desc");
        $this->db->limit($limit, $page);
        $query = $this->db->get();
        return $query->result_array();
    }
    
    function get_List($where){
        $this->db->select($this->plat.'.*');
        $this->db->from($this->plat);
        $this->db->where($where);
        $this->db->order_by("create_time", "desc");
        $query = $this->db->get();
        return $query->result_array(); 
    }
    
    function get_plat_info($where){
        $this->db->select($this->plat.'.*,'.$this->plat_info.'.*,'.$this->plat.".id platID");
        $this->db->from($this->plat);
        $this->db->join($this->plat_info, $this->plat_info.'.plat_en = '.$this->plat.'.name_en', 'left');
        $this->db->where($where);
        $this->db->order_by("plat.create_time", "desc");
        $query = $this->db->get();
        return $query->row_array(); 
    }
    
    function getPlatCount($where){
        $this->db->select('*');
        $this->db->from($this->plat);
        $this->db->where($where);
        $this->db->order_by("create_time", "desc");
        $this->db->group_by("name_en");
        $query = $this->db->get();
        return $query->num_rows();
    }
    
    function getPlatList($where,$page,$limit){
        $this->db->select($this->plat.'.*,'.$this->plat_info.'.*');
        $this->db->from($this->plat);
        $this->db->join($this->plat_info, $this->plat_info.'.plat_en = '.$this->plat.'.name_en', 'left');
        $this->db->where($where);
        $this->db->order_by("plat.create_time", "desc");
        $this->db->group_by("name_en");
        $this->db->limit($limit, $page);
        $query = $this->db->get();
        return $query->result_array(); 
    }
    
    function getCount($where){
        $this->db->select('*');
        $this->db->from($this->plat);
        $this->db->where($where);
        $this->db->order_by("create_time", "desc");
        $query = $this->db->get();
        return $query->num_rows();
    }
    
    function getList($where,$page,$limit){
        $this->db->select($this->plat.'.*');
        $this->db->from($this->plat);
        //$this->db->join($this->plat_info, $this->plat_info.'.plat_en = '.$this->plat.'.name_en', 'left');
        $this->db->where($where);
        $this->db->order_by("plat.create_time", "desc");
        $this->db->limit($limit, $page);
        $query = $this->db->get();
        return $query->result_array(); 
    }

    function get_distinct_plat(){
        $this->db->select($this->plat.'.*');
        $this->db->from($this->plat);
        $this->db->order_by("create_time", "desc");
        $this->db->group_by("name_en");
        $query = $this->db->get();
        return $query->result_array(); 
    }
    
    function get_plat_interface($where){
        $this->db->select($this->plat_interface.'.*');
        $this->db->from($this->plat_interface);
        $this->db->where($where);
        $query = $this->db->get();
        return $query->row_array(); 
    }
    
    function get_plat_rules($where){
        $this->db->select($this->plat_rule.'.*');
        $this->db->from($this->plat_rule);
        $this->db->where($where);
        $this->db->order_by("step", "asc");
        $query = $this->db->get();
        return $query->result_array(); 
    }
    
    function get_user_award_list($where){
        $this->db->select("SUM(award) award,SUM(true_award) true_award");
        $this->db->from($this->plat_log);
        $this->db->where($where);
        $this->db->order_by('create_time','desc');
        return $this->db->get()->row_array();
    }
    
    function get_pay_amount_all($where){
        $this->db->select("SUM(pay_amount_all) pay_amount_all");
        $this->db->from($this->plat);
        $this->db->where($where);
        $this->db->group_by('name_en');
        $this->db->order_by('create_time','desc');
        $result = $this->db->get()->row_array();
        return $result['pay_amount_all'];
    }
    
    function get_pay_amount($where){
        $this->db->select("pay_amount");
        $this->db->from($this->plat);
        $this->db->where($where);
        $this->db->order_by('create_time','desc');
        $result = $this->db->get()->row_array();
        return $result['pay_amount'];
    }
    
    function get_log_count($where){
        $this->db->select('*');
        $this->db->from($this->plat_log);
        $this->db->where($where);
        $this->db->order_by("create_time", "desc");
        $query = $this->db->get();
        return $query->num_rows();
    }
    
    function get_log_list($where,$page,$limit){
        $this->db->select($this->plat_log.'.*,'.$this->plat.'.name_ch,'.$this->plat.'.name_en,'.$this->users.'.username,'.$this->plat.".plat_period");
        $this->db->from($this->plat_log);
        $this->db->join($this->plat, $this->plat.'.id = '.$this->plat_log.'.plat_id', 'left');
        $this->db->join($this->users, $this->users.'.user_id = '.$this->plat_log.'.user_id', 'left');
        $this->db->where($where);
        $this->db->order_by("plat_log.create_time desc");
        $this->db->limit($limit, $page);
        $query = $this->db->get();
        return $query->result_array(); 
    }
    
    function get_cooperation_count($where,$like){
        $this->db->select('*');
        $this->db->from($this->plat_cooperation);
        $this->db->where($where);
        if($like){
            $this->db->like($this->plat_cooperation.'.company',$like);
        }
        $this->db->order_by("create_time", "desc");
        $query = $this->db->get();
        return $query->num_rows();
    }
    
     function get_cooperation_list($where,$page,$limit,$like){
        $this->db->select($this->plat_cooperation.'.*');
        $this->db->from($this->plat_cooperation);
        $this->db->where($where);
        if($like){
            $this->db->like($this->plat_cooperation.'.company',$like);
        }
        $this->db->order_by("create_time", "desc");
        $this->db->limit($limit, $page);
        $query = $this->db->get();
        return $query->result_array(); 
    }
    
    //插入平台数据
    function insert_plat($data)
    {
        $this->db->insert($this->plat, $data);
        if($this->db->affected_rows()){
           return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    
    
    function insert_plat_info($data){
        $this->db->insert($this->plat_info, $data);
        if($this->db->affected_rows()){
           return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    function insert_plat_interface($data){
        $this->db->insert($this->plat_interface, $data);
        if($this->db->affected_rows()){
           return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    function insert_plat_rule($data){
        $this->db->insert_batch($this->plat_rule, $data);
        if($this->db->affected_rows()){
           return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    function insert_plat_log($data){
        $this->db->insert($this->plat_log, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    //更新平台数据
    function update_plat($data,$plat_id)
    {
            return $this->db->where('id',$plat_id)->update($this->plat, $data);
    }
    
    //更新平台附加信息数据
    function update_plat_info($data,$plat_en)
    {
            return $this->db->where('plat_en',$plat_en)->update($this->plat_info, $data);
    }
    
    //更新平台附加信息数据
    function update_plat_interface($data,$plat_en)
    {
            return $this->db->where('plat_en',$plat_en)->update($this->plat_interface, $data);
    }
    function update_plat_cooperation($data,$id){
        return $this->db->where('id',$id)->update($this->plat_cooperation, $data);
    }
    //删除数据
    function dalete($delid)
    {	
            $this->db->where('id',$delid);
            $this->db->delete($this->table);
            return $this->db->affected_rows();
    }
    
    function delete_plat_rule($plat_id){
            $this->db->where('plat_id',$plat_id);
            $this->db->delete($this->plat_rule);
            return $this->db->affected_rows();
    }
    
    function delete_cooperation($delid){
        $this->db->where('id',$delid);
        $this->db->delete($this->plat_cooperation);
        return $this->db->affected_rows();
    }
    
    //设置状态
    function setstatus($status,$ids)
    {
            return $this->db->where_in('id',$ids)->update($this->table, array('status'=>$status));
    }
     /******************************/
    function get_baseinfo($where){
        $this->db->select($this->plat_baseinfo.'.*');
        $this->db->from($this->plat_baseinfo);
        $this->db->where($where);
        $query = $this->db->get();
        return $query->row_array(); 
    }
    
    function insert_baseinfo($data){
        $this->db->insert($this->plat_baseinfo, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    function update_baseinfo($data,$plat_en){
        return $this->db->where('plat_en',$plat_en)->update($this->plat_baseinfo, $data);
    }
    /******************************/
    /*******************************/
    function getPlatCategoryList($where){
        $this->db->select("*");
        $this->db->from("tan_plat_category");
        $this->db->where($where);
        $this->db->order_by("sort desc");
        $query = $this->db->get();
        return $query->result_array(); 
    }
    /*******************************/
    //递归生成树形数组
    function tree($table,$pid=0){
        $tree = array();
        foreach($table as $row){
            if($row['pid']==$pid){
                $tmp = $this->tree($table,$row['id']);
                if($tmp){
                        $row['children']=$tmp;
                }else{
                        $row['leaf'] = true;
                }
                $tree[]=$row;                
            }
        }
        return $tree;
    }
}
